<?php

//http://blog.sosedoff.com/2009/09/20/rails-like-php-url-router/
class ecRouter{
  protected $applicationName,
            $moduleName,
            $controllerName,
            $actionName;
  
  public $requestUri;
  public $routes;
  public $application;
  public $module;
  public $controller;
  public $controllerNameLink;
  public $action;
  public $id;
  public $values;
  public $routeFound = false;

  public function __construct()
  {
    $request = $_SERVER['REQUEST_URI'];
    $pos = strpos($request, '?');
    if ($pos) $request = substr($request, 0, $pos);

    $this->requestUri = $request;
    $this->routes = array();
  }

  public function map($rule, $target=array(), $conditions=array())
  {
    $this->routes[$rule] = new ecRoute($rule, $this->requestUri, $target, $conditions);
  }

  public function defaultRoute()
  {
    $this->map('/:application/:module/:controller/:action');
  }

  private function setRoute($route)
  {
    $this->routeFound = true;
    $values = $route->values;

    if(array_key_exists('application', $values))
      $this->application = $values['application'];
    unset($values['application']);
    
    if(array_key_exists('module', $values))
      $this->module = $values['module'];
    unset($values['module']);
    
    if(array_key_exists('controller', $values))
      $this->controller = $values['controller'];
    unset($values['controller']);

    if(array_key_exists('action', $values))
      $this->action = $values['action'];
    unset($values['action']);

    if(array_key_exists('id', $values))
      $this->id = $values['id'];
    
    $this->values = array_merge($values, $_GET);

    $w = explode('_', $this->controller);

    foreach($w as $k => $v) $w[$k] = ucfirst($v);
      $this->controllerNameLink = implode('', $w);
  }

  public function execute()
  {
    foreach($this->routes as $route)
    {
      if ($route->isMatched)
      {
        $this->setRoute($route);
        $this->setRouteController($route);
        break;
      }
    }
  }


  public function setRouteController($route)
  {
    try{
      $this->setApplicationName($route);
      $this->setModuleName($route);
      $this->setControllerName($route);
      $this->setActionName($route);
      return true;
    }
    catch(Exception $e)
    {
      throw new Exception('Error in initialisation of controllers');
    }
  }

  public function getArrayRouteController()
  {
    if(is_array($this->values))
    {
      return array_merge($this->values, array(
        'applicationName' => $this->getApplicationName(),
        'moduleName'      => $this->getModuleName(),
        'controllerName' => $this->getControllerName(),
        'actionName' => $this->getActionName(),
        ));
    }
    else{
    return array(
      'applicationName' => $this->getApplicationName(),
      'moduleName'      => $this->getModuleName(),
      'controllerName' => $this->getControllerName(),
      'actionName' => $this->getActionName(),
      );
    }
  }

  public function setControllerName($route)
  {
    $this->controllerName = $route->values['controller'];
  }

  public function setModuleName($route)
  {
    $this->moduleName = $route->values['module'];
  }

  public function setApplicationName($route)
  {
    $this->applicationName = $route->values['application'];
  }

  public function setActionName($route)
  {
    $this->actionName = $route->values['action'];
  }

  public function getControllerName()
  {
    return $this->controllerName;
  }

  public function getModuleName()
  {
    return $this->moduleName;
  }

  public function getApplicationName()
  {
    return $this->applicationName;
  }

  public function getActionName()
  {
    return $this->actionName;
  }

  public function getApplicationRoute($ref)
  {
    global $routes;

    if(array_key_exists($ref, $routes))
    {
      return $routes[$ref]['param']['application'];
    }
    else{
      return false;
    }
  }
}